package com.sgb.kjwl.view.ui.activity.self.goods;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityChoiceVipPriceGoodsBinding;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.view.ui.adapter.self.goods.ChoiceVipPriceGoodsAdpater;
import com.sgb.kjwl.viewmodel.self.goods.ChoiceVipPriceGoodsViewModel;
import com.sgb.link.router.PathGoods;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.common.utils.ActivityJump;

@Route(path = PathGoods.ChoiceVipPriceGoodsActivity)
public class ChoiceVipPriceGoodsActivity extends BaseActivity<ChoiceVipPriceGoodsViewModel, ActivityChoiceVipPriceGoodsBinding> {
    public static final int OK = 1158;

    @Override
    protected int layoutId() {
        return R.layout.activity_choice_vip_price_goods;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(ChoiceVipPriceGoodsViewModel.class);
        mBinding.setVariable(BR.ViewModel, mViewModel);
    }


    private ChoiceVipPriceGoodsAdpater madpater;


    @Override
    protected void init() {
        initRecyclerView();
        initObserve();
        mViewModel.onRefresh();
        mBinding.actionBar.setOnBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBinding.actionBar.setMenuFunctionClick(view -> {

            if (madpater.choice_position == -1) {
                mViewModel.mToast.postValue("请选择商品");
                return;
            }

            mViewModel.getGoosType(madpater.getDatas().get(madpater.choice_position).getId());

        });
    }

    private void initRecyclerView() {
        madpater = new ChoiceVipPriceGoodsAdpater(this);
        mBinding.recyclerview.setLoadingListener(mViewModel);
        mBinding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerview.setAdapter(madpater);
    }


    private void initObserve() {
        mViewModel.listdata.observe(this, listdata -> {
            if (mViewModel.isloadmore) {
                if (null != listdata && listdata.size() > 0) {
                    mBinding.recyclerview.loadMoreComplete();
                    madpater.addDatas(listdata);
                } else {
                    mBinding.recyclerview.setNoMore(true);
                }
                mBinding.recyclerview.setVisibility(View.VISIBLE);
            } else {
                mBinding.recyclerview.refreshComplete();
                if (listdata.size() == 0) {
                    madpater.setDatasMayNull(listdata);
                    mBinding.errorView.setVisibility(View.VISIBLE);
                } else {
                    mBinding.errorView.setVisibility(View.GONE);
                    mBinding.recyclerview.setVisibility(View.VISIBLE);
                    madpater.setDatas(listdata);
                }
            }
        });

        mViewModel.dialog_show_text.observe(this, data -> {
            if (!TextUtils.isEmpty(data)) {
                DialogHelper.showDialog(this, "温馨提示", data, null, "知道了", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        mViewModel.onRefresh();
                    }
                });
            }
        });
        mViewModel.ok.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String itemId) {
                ActivityJump.startActivityForResult(ChoiceVipPriceGoodsActivity.this, OK, PathGoods.AddMemberGoodsActivity, Pair.create("itemId", itemId));
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case OK:
                finish();
                break;
        }
    }
}
